<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_paper {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tblpapers";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function get_default_description() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbloption WHERE id = 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function set_default_description($default_description) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "UPDATE tbloption SET paper_default_description = '" . $default_description . "' WHERE id = 1";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function add(
    $paper_id, $title, $author, $citation, $keywords, $article_type, $pages, $doi, $publisher, $acknown, $pdf, $abstract, $detail_article, $dated_by_user, $cat_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "CALL addPapers(" . $paper_id . ", '" . $title . "', '" . $author . "', '" . $citation . "', '" . $keywords . "', '" . $article_type . "', '" . $pages . "', '" . $doi . "', '" . $publisher . "', '" . $acknown . "', '" . $abstract . "', '" . $detail_article . "', '" . $pdf . "', '" . lib_date::get_now() . "', '" . $dated_by_user . "', '" . $cat_id . "')";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "CALL GetPapersById(" . $id . ")";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($paper_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tblpapers WHERE PaperID = " . $paper_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    function get_all_archive_by_journal_id($journal_id) {
        $papers = $this->get_all_by_journal_id_with_datebyuser_desc_sort($journal_id);
        $archives = array();
        $count = 0;
        foreach ($papers as $i => $item) {
            if (!empty($item["DateByUser"])) {
                $date = date_create($item["DateByUser"]);
                $month = date_format($date, 'm');
                $year = date_format($date, 'Y');
                $date_string = date_format($date, 'F, Y');
                $archive = array();
                $archive['month'] = $month;
                $archive['year'] = $year;
                $archive['date_string'] = $date_string;
                $archive['JournalID'] = $item["JournalID"];
                if ($count == 0 || $archive['date_string'] != $archives[$count - 1]['date_string']) {
                    $archives[$count] = $archive;
                    $count = $count + 1;
                }
            }
        }
        return $archives;
    }

    function get_years($journal_id) {
        $years = array();
        $count = 0;
        $archive = $this->get_all_archive_by_journal_id($journal_id);
        for ($index = 0; $index < sizeof($archive); $index++) {
            for ($index1 = 0; $index1 < $index; $index1++) {
                if ($archive[$index]['year'] == $archive[$index1]['year']) {
                    break;
                }
            }

            if ($index == $index1) {
                $years[$count] = $archive[$index];
                $count++;
            }
        }
        return $years;
    }

    function get_all_by_journal_id($journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "CALL GetAllPapersByJournalID(" . $journal_id . ")")
                or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    function get_all_by_journal_id_with_datebyuser_desc_sort($journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "SELECT * FROM tblpapers WHERE JournalID = " . $journal_id . " ORDER BY DateByUser DESC")
                or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function get_all_by_journal_id_and_date($journal_id, $month, $year) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "CALL GetAllPapersByJournalIDnDated(" . $journal_id . ",'" . $month . "','" . $year . "')")
                or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

}
